package com.hunttown.mes.manage.controller._mes;

import com.hunttown.mes.manage.controller.common.ModelService;
import com.hunttown.mes.manage.controller.vo.ShowInfoVo;
import com.hunttown.mes.manage.service.MesOrderMaterialManageService;
import com.hunttown.mes.manage.service.MesSaleOrderManageService;
import com.hunttown.mes.rpc.domain.MesOrderMaterialDTO;
import com.hunttown.mes.rpc.domain.MesSaleOrderDTO;
import com.hunttown.mes.rpc.domain.enums.purchase.ProductTypeEnum;
import com.hunttown.mes.rpc.domain.query.MesOrderMaterialDTOQuery;
import com.hunttown.common.domain.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;

import static com.hunttown.mes.common.utils.PageUtils.PagePackage;

/**
 * created by wangjunfu on 2022-03-30 15:27:00
 */
@Controller
@RequestMapping(value = "/mes/order-material")
public class MesOrderMaterialController {

    private final static Logger logger = LoggerFactory.getLogger(MesOrderMaterialController.class);

    private final MesOrderMaterialManageService mesOrderMaterialService;
    private final MesSaleOrderManageService mesSaleOrderService;
    private final ModelService modelService;

    @Autowired
    public MesOrderMaterialController(MesOrderMaterialManageService mesOrderMaterialService, MesSaleOrderManageService mesSaleOrderService, ModelService modelService) {
        this.mesOrderMaterialService = mesOrderMaterialService;
        this.mesSaleOrderService = mesSaleOrderService;
        this.modelService = modelService;
    }

    //region 数据列表

    /**
     * 首页
     *
     * @param model    model模型
     * @param dtoQuery 查询条件
     * @return model
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model, MesOrderMaterialDTOQuery dtoQuery) {

        dtoQuery.setTableId(203); //销售订单
        dtoQuery.setOrderBy("order by id desc");
        Page<MesOrderMaterialDTO> dataPage = mesOrderMaterialService.getForPage(dtoQuery);

        for (MesOrderMaterialDTO item : dataPage.getItems()) {
            item.setGoodsTypeName(ProductTypeEnum.getEnumByKey(item.getGoodsType()));

            MesSaleOrderDTO order = mesSaleOrderService.getById(item.getRecordId());
            if (order == null) {
                continue;
            }
            item.setOrderName(order.getOrderTitle());
            item.setOrderDateSr(order.getOperateDateSr());

            if (order.getCompanyId() == null || order.getCompanyId() <= 0) {
                continue;
            }
            item.setCompanyName(modelService.getCompanyName(order.getCompanyId()));
        }

        model.addAttribute("page", dataPage);
        model.addAttribute("PagePackage", PagePackage(dataPage.getPn(), dataPage.getStart(), dataPage.getPageSize(), dataPage.getTotalPages(), dataPage.getTotalItems()));
        model.addAttribute("dtoQuery", dtoQuery);

        return "mes/order-material/index";
    }
    //endregion

    //region 数据查看

    /**
     * 数据查看
     *
     * @param model     model模型
     * @param id        主键id
     * @param tableName 数据库英文表名
     * @return model
     */
    @RequestMapping(value = "/show", method = RequestMethod.POST)
    public String show(Model model, Integer id, String tableName) {
        id = (id == null ? 0 : id);

        MesOrderMaterialDTO obj = new MesOrderMaterialDTO();
        obj.setId(id);

        if (id > 0) {
            obj = mesOrderMaterialService.getById(id);
        }
        model = mesOrderMaterialService.assembleObjInfo(model, tableName, obj);

        return "show/show-template";
    }

    //详情
    @RequestMapping(value = "/getInfo", method = RequestMethod.POST)
    public String getInfo(Model model, Integer id) {

        //显示标题
        String showTitle = "未知";

        //数据
        ShowInfoVo vo;
        List<ShowInfoVo> dataList = new ArrayList<>();

        if (id != null && id > 0) {
            MesOrderMaterialDTO obj = mesOrderMaterialService.getById(id);

            if (obj != null) {
                vo = new ShowInfoVo();
                vo.setKey("编号ID");
                vo.setValue(obj.getId());
                dataList.add(vo);

                MesSaleOrderDTO order = mesSaleOrderService.getById(obj.getRecordId());
                if (order != null) {
                    showTitle = order.getOrderTitle();

                    vo = new ShowInfoVo();
                    vo.setKey("订单名称");
                    vo.setValue(order.getOrderTitle());
                    dataList.add(vo);

                    vo = new ShowInfoVo();
                    vo.setKey("订单编号");
                    vo.setValue(order.getOrderNo());
                    dataList.add(vo);

                    vo = new ShowInfoVo();
                    vo.setKey("客户名称");
                    vo.setValue(modelService.getCompanyName(order.getCompanyId()));
                    dataList.add(vo);

                    vo = new ShowInfoVo();
                    vo.setKey("订单日期");
                    vo.setValue(order.getOperateDateSr());
                    dataList.add(vo);
                }

                vo = new ShowInfoVo();
                vo.setKey("原料名称");
                vo.setValue(obj.getMaterialName());
                dataList.add(vo);

                vo = new ShowInfoVo();
                vo.setKey("原料编号");
                vo.setValue(obj.getMaterialNo());
                dataList.add(vo);

                vo = new ShowInfoVo();
                vo.setKey("原料类型");
                vo.setValue(ProductTypeEnum.getEnumByKey(obj.getGoodsType()));
                dataList.add(vo);

                vo = new ShowInfoVo();
                vo.setKey("规格型号");
                vo.setValue(obj.getSpecialName());
                dataList.add(vo);

                vo = new ShowInfoVo();
                vo.setKey("原料数量");
                vo.setValue(obj.getCount());
                dataList.add(vo);
            }

            model.addAttribute("showTitle", showTitle);
            model.addAttribute("dataList", dataList);
        }

        return "show/getinfo-template";
    }

    //endregion

}